package com.choosefine.base.shixiaobao.common.conf;

import java.sql.SQLException;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import com.alibaba.druid.pool.DruidDataSource;
import com.choosefine.base.shixiaobao.common.model.CustomRestTemplate;

@Configuration
@MapperScan(basePackages = {"com.choosefine.it.construction.**.dao","com.choosefine.it.construction.**.mapping"})
@EnableTransactionManagement
public class MybatisConfig implements TransactionManagementConfigurer{

	private static final Logger logger = LoggerFactory.getLogger(MybatisConfig.class);

	@Autowired
	private DruidDBConfig druidDBConfig;

	@Autowired
	DataSource dataSource;
	
	// 声明其为Bean实例
	@Bean
	// 在同样的DataSource中，首先使用被标注的DataSource
	@Primary
	public DataSource dataSource() {

		DruidDataSource datasource = new DruidDataSource();

		datasource.setUrl(druidDBConfig.getUrl());
		datasource.setUsername(druidDBConfig.getUsername());
		datasource.setPassword(druidDBConfig.getPassword());
		datasource.setDriverClassName(druidDBConfig.getDriverClassName());

		// configuration
		datasource.setInitialSize(druidDBConfig.getInitialSize());
		datasource.setMinIdle(druidDBConfig.getMinIdle());
		datasource.setMaxActive(druidDBConfig.getMaxActive());
		datasource.setMaxWait(druidDBConfig.getMaxWait());
		datasource.setTimeBetweenEvictionRunsMillis(druidDBConfig
				.getTimeBetweenEvictionRunsMillis());
		datasource.setMinEvictableIdleTimeMillis(druidDBConfig
				.getMinEvictableIdleTimeMillis());
		datasource.setValidationQuery(druidDBConfig.getValidationQuery());
		datasource.setTestWhileIdle(druidDBConfig.isTestWhileIdle());
		datasource.setTestOnBorrow(druidDBConfig.isTestOnBorrow());
		datasource.setTestOnReturn(druidDBConfig.isTestOnReturn());
		datasource.setPoolPreparedStatements(druidDBConfig
				.isPoolPreparedStatements());
		datasource.setMaxPoolPreparedStatementPerConnectionSize(druidDBConfig
				.getMaxPoolPreparedStatementPerConnectionSize());
		try {
			datasource.setFilters(druidDBConfig.getFilters());
		} catch (SQLException e) {
			logger.error("druid configuration initialization filter", e);
		}
		datasource.setConnectionProperties(druidDBConfig
				.getConnectionProperties());

		return datasource;
	}

	@Bean
	public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory){
		return new SqlSessionTemplate(sqlSessionFactory);
	}

	@Bean
	public PlatformTransactionManager annotationDrivenTransactionManager() {
		logger.debug("> transactionManager");
		return new DataSourceTransactionManager(dataSource);
	}

	@PostConstruct
	public void postConstruct() {
		logger.info("jdbc.settings={}", druidDBConfig);
	}
	@Bean
	CustomRestTemplate restTemplate(){
		return new CustomRestTemplate();
	}
}
